package cn.wk.service.impl;

import cn.wk.AjaxResult;
import cn.wk.RedisUtils;
import cn.wk.ValidUtils;
import cn.wk.domain.Movie;
import cn.wk.dto.MovieDto;
import cn.wk.exception.GlobalException;
import cn.wk.mapper.MovieMapper;
import cn.wk.service.IMovieService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.sql.rowset.BaseRowSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wk
 * @since 2021-03-18
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements IMovieService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 电影服务提供今日电影查询接口，返回今日上线的电影列表
     *
     * @return
     */
    @Override
    public List<Movie> findToday() {
        return baseMapper.findToday();
    }

    /**
     * 购票服务提供电影票购买接口，用户可以通过该接口购买电影票，同时保存购票记录
     *
     * @param movie
     */
    @Override
    public Movie goBuy(Movie movie) {
        //先判断是否有购买电影票
        ValidUtils.assertNotNull(movie, "电影票参数错误，请重试");
        //根据id去数据
        Movie dbMovie = baseMapper.selectById(movie.getId());
        //判断电影是否上线
        ValidUtils.assertLongEq(dbMovie.getState(),0,"该电影还未上线");
        //判断下是否有该id的电影票
        ValidUtils.assertNotNull(dbMovie,"电影票参数错误");
        ValidUtils.assertNotNull(dbMovie,"电影票参数错误");
        return dbMovie;

    }

    /**
     * 更新票数
     * @param movieDto
     */
    @Override
    public void updateTicked(MovieDto movieDto) {
        Integer number = movieDto.getNumber();
        Long movielId = movieDto.getMovielId();
        //根据id查询
        Movie movie = baseMapper.selectById(movieDto.getMovielId());
        Integer ticketNumber = movie.getTicketNumber();
        number = ticketNumber - number;
        //判断参数
        ValidUtils.assertNotNull(number,"票数异常");
        ValidUtils.assertNotNull(movielId,"电影id异常");
        baseMapper.updateTicked(number,movielId);
    }

    /**
     * 电影上线并存入redis中
     * @param movieDto
     */
    @Override
    public void online(MovieDto movieDto) {
        //校验数据
        Long movielId = movieDto.getMovielId();
        Integer state = movieDto.getState();
        ValidUtils.assertNotNull(movielId,"请选择要上线的电影");
        //根据id去查询要上线的电影
        Movie movie = baseMapper.selectById(movielId);
        ValidUtils.assertLongEq(movie.getState(),1,"已经上线不能重复线上");
        //把查询出来的该电影状态进行更改
        movie.setOnlineDate(movieDto.getOnlineTime());
        movie.setState(1);
        HashMap<String, Movie> map = new HashMap();
        map.put(movie.getId().toString(), movie);
        //以hash结构保存到redis中
        redisTemplate.opsForHash().putAll("movie",map);
        //从redis中取值
        List o = redisTemplate.opsForHash().values("movie");
        //System.out.println(o);
        //更改数据库状态
        baseMapper.updateStatu(movie);
    }

    /**
     *查询今日电影从Reids中进行查询
     */
    public List<Movie> fromRedis(){
        return redisTemplate.opsForHash().values("movie");
    }

    /**
     * 从redis中购票
     */
    public void fromRedisBuy(MovieDto movieDto){
        List movies = redisTemplate.opsForHash().values("movie");
        System.out.println(movieDto);
        System.out.println("-----------------------------------------");
        System.out.println(movies);
        HashMap<String, Movie> map = new HashMap();
        movies.forEach(movie->{
            Movie mo = (Movie) movie;
            System.out.println(mo.getId());
            if(mo.getId().intValue() == movieDto.getMovielId().intValue() && mo.getTicketNumber()>movieDto.getNumber()){
                System.out.println("进if了吗");
                Integer i = mo.getTicketNumber()-movieDto.getNumber();
                mo.setTicketNumber(i);
                map.put(mo.getId().toString(), mo);
                redisTemplate.opsForHash().putAll("movie",map);
                return;
            }
        });
    }
}
